package com.topgay.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.topgay.domain.AnswerRaw;
import com.topgay.domain.MatchInfo;
import com.topgay.domain.Member;
import com.topgay.service.DatingService;
import com.topgay.service.MemberService;

@Controller
@RequestMapping("/dating/")
public class DatingController {

	@Autowired
	private DatingService service;

	// 첫 시작페이지로 넘어가는 거
	// 로그인 정보가 없으면 경고창을 띄운다.
	@RequestMapping("startPage.do")
	public String startPage(HttpSession session, HttpServletResponse response) throws IOException {
		Object obj = session.getAttribute("login_info");

		if (obj != null) {
			//이전 설문 결과가 있는지 체크해서 있으면 널을 리턴해야 한다.
			if(service.checkAnswer(((Member) obj).getId()) != 0){
				response.setContentType("Text/html;charset=utf-8");
				PrintWriter writer = response.getWriter();
				writer.println("<script type='text/javascript'>");
				writer.println("alert('이미 설문을 했습니다.');");
				writer.println("history.back();");
				writer.println("</script>");
				writer.flush();
				return null;
			}else{
				return "dating/start";
			}
		}else {
			response.setContentType("Text/html;charset=utf-8");
			PrintWriter writer = response.getWriter();
			writer.println("<script type='text/javascript'>");
			writer.println("alert('로그인을 해주세요.');");
			writer.println("history.back();");
			writer.println("</script>");
			writer.flush();
			return null;
		}
	}
	
	@RequestMapping("checkAnswer.do")
	public String checkAnswer(HttpSession session, HttpServletResponse response) throws IOException {
		Object obj = session.getAttribute("login_info");
		
		if (obj != null) {
			//이전 설문 결과가 있는지 체크해서 있으면 널을 리턴해야 한다.
			if(service.checkAnswer(((Member) obj).getId()) == 0){
				response.setContentType("Text/html;charset=utf-8");
				PrintWriter writer = response.getWriter();
				writer.println("<script type='text/javascript'>");
				writer.println("alert('먼저 설문을 진행하세요.');");
				writer.println("</script>");
				writer.flush();
				return "dating/start";
			}else{
				return "/TopGay/dating/searchList.do?"+((Member) obj).getId();
			}
		}else {
			response.setContentType("Text/html;charset=utf-8");
			PrintWriter writer = response.getWriter();
			writer.println("<script type='text/javascript'>");
			writer.println("alert('로그인을 해주세요.');");
			writer.println("</script>");
			writer.flush();
			return null;
		}
	}
	

	/**
	 * 설문 내용을 받아서 디비에 전송 정상적으로 디비에 값이 들어가면 그 값을 가지고 설문완료 페이지로 가서 설문 완료 메시지를 띄운다.
	 */
	@RequestMapping("answer.do")
	public ModelAndView answer(AnswerRaw answer, String id, String nickname) throws Exception {
		int check = service.setAnswerRaw(answer, id, nickname);
		return new ModelAndView("dating/sucess", "check", check);
	}
	
	/**
	 * 다른 회원들과 설문을 결과를 비교해 % 수치로 값을 내고 높은 순서대로 return
	 * @param String
	 * @return List보여주는 page, MatchInfo List
	 */
	@RequestMapping("searchList.do")
	public String searchList(String id, ModelMap map){
		ArrayList<MatchInfo> list = service.searchList(id);
		
		int totalAnswerMember = list.size();
		int count = 0;
		int countFlag = 0;
		
		for(MatchInfo tmp : list){
			if(tmp.getMatchPercent() > 50){
				count ++;
			}
		}
		
		if(count >  (totalAnswerMember/2)){
			countFlag = 1;
		}
		
		map.addAttribute("countFlag", countFlag);
		map.addAttribute("list", list);
		
		return "dating/match_list";
	}
	
	@RequestMapping("datingIndex.do")
	public String datingPage(){
		return "dating/match_list";
	}
	
	@RequestMapping("timeLineList.do")
	public String timeLineList(String id, ModelMap map){
		ArrayList<MatchInfo> list = service.timeLineList(id);
		map.addAttribute("list", list);
		return "jsonView";
	}
	
	@RequestMapping("checkTimeLine.do")
	public String checkTimeLine(HttpSession session, ModelMap map){
		ArrayList<MatchInfo> list = service.timeLineList(((Member) session.getAttribute("login_info")).getId());
		map.addAttribute("list", list);
		return "jsonView";
	}
}
